package com.guzhen.wallpager;

import android.annotation.SuppressLint;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.text.TextUtils;

import com.fun.funlivewallpaper.AppApplication;
import com.fun.funlivewallpaper.R;
import com.fun.funlivewallpaper.WallpaperHelper;

import java.io.File;
import java.io.FileOutputStream;

public class WallpaperBitmapHelper {
    public static class InstanceH {
        private static WallpaperBitmapHelper instance;

        static {
            InstanceH.instance = new WallpaperBitmapHelper();
        }
    }

    private int screenHeight;
    private int screenWidth;
    private Bitmap def_wallpaper_bitmap;
    private Bitmap reveiew_bg_bitmap;

    public static WallpaperBitmapHelper getInstance() {
        return InstanceH.instance;
    }

    private Bitmap backgroundBitmap;
    private Bitmap maskingGuide1Bitmap;


    private WallpaperBitmapHelper() {
        this.screenHeight = ScreenUtils.getScreenHeight();
        this.screenWidth = ScreenUtils.getScreenWidth();
    }


    public Bitmap getWallpaperBackgroundBitmap() {
        if (this.backgroundBitmap == null) {
            this.initWallpaperBitmapBackground(AppApplication.getApplication());
        }

        return this.backgroundBitmap;
    }

    public Bitmap getMaskingGuide1Bitmap() {
        if (this.maskingGuide1Bitmap == null) {
            this.initMaskingGuide1Bitmap(AppApplication.getApplication());
        }

        return this.maskingGuide1Bitmap;
    }

    private void initMaskingGuide1Bitmap(Context context0) {
        if (this.maskingGuide1Bitmap == null) {
            this.getMaskingGuide1Bitmap(context0);
        }
    }

    private void getMaskingGuide1Bitmap(Context context0) {
        if (this.maskingGuide1Bitmap == null) {
            this.maskingGuide1Bitmap = this.extractThunbnailBitmap(BitmapFactory.decodeResource(context0.getResources(), R.drawable.gz_step_guilde_wallpager_2));
        }
    }

    private void initWallpaperBitmapBackground(Context context0) {
        if (this.backgroundBitmap == null) {
            this.initDefaultWallpaperBitmap(context0);
        }
    }

    private void initDefaultWallpaperBitmap(Context context0) {
        if (this.backgroundBitmap == null) {
            this.backgroundBitmap = this.extractThunbnailBitmap(BitmapFactory.decodeResource(context0.getResources(), R.drawable.gz_step_guilde_wallpager_1));
        }
    }

    private Bitmap extractThunbnailBitmap(Bitmap bitmap0) {
        int v = bitmap0.getWidth();
        int v1 = bitmap0.getHeight();
        return ThumbnailUtils.extractThumbnail(bitmap0, this.screenWidth, ((int) (((float) this.screenWidth) * (((float) v1) / ((float) v)))));
    }

    public Bitmap getUserDefautWallpaper() {
        if (this.def_wallpaper_bitmap == null) {
            this.getCachedUserOrgWallpaper(AppApplication.getApplication());
        }

        return this.def_wallpaper_bitmap;
    }

    private void getCachedUserOrgWallpaper(Context context0) {
        Throwable throwable1;
        Exception exception2;
        FileOutputStream fileOutputStream0;
        String defaultWallpaperPath = WallpaperHelper.INSTANCE.getDefaultWallpaperPath(context0);
        if (TextUtils.isEmpty(defaultWallpaperPath)) {
            defaultWallpaperPath = context0.getFilesDir() + "/wall.jpeg";
        }
        File file0 = new File(defaultWallpaperPath);
        if (!file0.exists()) {
            WallpaperManager wallpaperManager0 = (WallpaperManager) context0.getSystemService("wallpaper");
            if (wallpaperManager0 != null) {
                try {
                    @SuppressLint("MissingPermission") Drawable drawable0 = wallpaperManager0.getDrawable();
                    if ((drawable0 instanceof BitmapDrawable)) {
                        this.screenHeight = ScreenUtils.getScreenHeight();
                        this.screenWidth = ScreenUtils.getScreenWidth();
                        this.def_wallpaper_bitmap = ThumbnailUtils.extractThumbnail(((BitmapDrawable) drawable0).getBitmap(), this.screenWidth, this.screenHeight);
                    }
                } catch (Exception exception0) {
                    exception0.printStackTrace();
                }
            }
        }

        if (this.def_wallpaper_bitmap == null && (file0.exists())) {
            this.def_wallpaper_bitmap = BitmapFactory.decodeFile(file0.getAbsolutePath());
        }

        try {
            fileOutputStream0 = new FileOutputStream(file0);
        } catch (Exception exception1) {
            fileOutputStream0 = null;
            exception2 = exception1;
        } catch (Throwable throwable0) {
            fileOutputStream0 = null;
            throwable1 = throwable0;
        }

        try {
            this.def_wallpaper_bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fileOutputStream0);
            fileOutputStream0.close();
        } catch (Exception except) {
            except.printStackTrace();
        }

    }


    public Bitmap getreveiew_bg_bitmap() {
        if(this.reveiew_bg_bitmap == null) {
            this.initReviewBitmap(AppApplication.getApplication());
        }

        return this.reveiew_bg_bitmap;
    }
    private void initReviewBitmap(Context context0) {
        this.reveiew_bg_bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeResource(context0.getResources(), R.drawable.reveiew_bg), this.screenWidth, this.screenHeight);
    }
}